package com.lihaogn.sell.web;

import com.lihaogn.sell.vo.SearchPageVo;
import com.lihaogn.sell.entity.Admin;
import com.lihaogn.sell.service.AdminService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * @FileName: AdminController
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-05-12 21:31
 * @Version: 1.0
 */
@Controller
@Api(tags = "管理员数据接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 记录adminId
     */
    String adminIdTmp;

    // ======================= 访问页面 =======================

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @ApiIgnore
    @RequestMapping(value = "/")
    private String login() {
        return "login";
    }

    @ApiIgnore
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    private String index() {
        return "index";
    }

    @ApiIgnore
    @RequestMapping(value = "welcome", method = RequestMethod.GET)
    private String welcome() {
        return "welcome";
    }

    @ApiIgnore
    @RequestMapping(value = "/adminshow", method = RequestMethod.GET)
    private String adminShow() {
        return "admin/admin-list";
    }

    @ApiIgnore
    @RequestMapping(value = "/adminadd", method = RequestMethod.GET)
    private String adminAdd() {
        return "admin/admin-add";
    }

    @ApiIgnore
    @RequestMapping(value = "/adminEdit", method = RequestMethod.GET)
    private String adminEdit(String adminId) {

        adminIdTmp = adminId;

        return "admin/admin-edit";

    }

    // ======================= 操作 =======================


    // ========== 删除 ==========

    /**
     * 批量删除admin
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "批量删除admin")
    @ApiResponse(code = 200, message = "删除成功")
    @RequestMapping(value = "/deleteMultiAdmin", method = RequestMethod.DELETE)
    @ResponseBody
    private Integer adminDeleteMulti(HttpServletRequest request) throws IOException{


        int resNum = adminService.deleteAdminsInNames(request);

        return resNum;


    }

    /**
     * 根据name删除一个admin
     *
     * @param adminName
     * @return
     */
    @ApiOperation(value = "删除admin", notes = "根据用户名删除admin")
    @ApiResponse(code = 200, message = "删除成功")
    @DeleteMapping(value = "/deleteAdmin")
    @ResponseBody
    private Integer adminDelete(String adminName) {
        return adminService.deleteAdminByName(adminName);
    }

    // ========== 修改 ==========

    /**
     * 更新admin
     *
     * @param admin
     * @return
     */
    @ApiOperation(value = "修改admin")
    @RequestMapping(value = "/updateAdmin", method = RequestMethod.PUT)
    @ResponseBody
    private Integer updateAdmin(Admin admin) {

        return adminService.updateAdmin(admin);
    }

    /**
     * 改变用户启用状态
     *
     * @param adminId
     * @param adminStatus
     * @return
     */
    @ApiOperation(value = "改变admin状态")
    @ApiResponse(code = 200, message = "修改成功")
    @RequestMapping(value = "/changeAdminStatus", method = RequestMethod.PUT)
    @ResponseBody
    private Integer changeAdminStatus(String adminId, String adminStatus) {

        int oldAdminStatus = Integer.valueOf(adminStatus);
        // 获取新的状态值
        int newAdminStatus;

        if (oldAdminStatus == 0) {
            newAdminStatus = 1;
        } else {
            newAdminStatus = 0;
        }

        Admin admin = new Admin();
        admin.setAdminStatus(newAdminStatus);
        admin.setAdminId(adminId);

        // 返回结果
        return adminService.updateAdmin(admin);
    }

    // ========== 插入 ==========

    /**
     * 添加admin
     *
     * @param admin
     * @return
     */
    @ApiOperation(value = "添加admin")
    @ApiResponse(code = 200, message = "添加成功")
    @RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
    @ResponseBody
    private Integer addAdmin(Admin admin) {

        int i = adminService.insertAdmin(admin);

        if (i == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    // ========== 查询 ==========

    /**
     * admin登录，验证账号和密码
     *
     * @param adminName
     * @param adminPassword
     * @return
     */
    @PostMapping(value = "/loginAdmin")
    @ResponseBody
    private Integer loginAdmin(String adminName, String adminPassword) {

        try {
            Admin admin = adminService.getAdminByName(adminName);
            if (admin.getAdminPassword().equals(adminPassword)) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        return 0;
    }

    /**
     * 展示所有admin
     *
     * @return
     */
    @ApiOperation(value = "显示admin列表", notes = "分页显示admin")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "第几页"), @ApiImplicitParam(name = "limit", value = "每页显示条数")})
    @RequestMapping(value = "/listadmin", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> showAdmin(String page, String limit) {

        // 分页数据
        Map<String, Object> modelMap = adminService.queryAdminForPage(page, limit);



        return modelMap;
    }

    /**
     * 返回匹配的admin的个数
     * 查询admin_name是否已经存在
     *
     * @param adminName
     * @return
     */
    @ApiOperation(value = "查询admin", notes = "根据name查询admin")
    @RequestMapping(value = "/queryAdminName", method = RequestMethod.GET)
    @ResponseBody
    private Integer queryAdminName(String adminName) {

        int num = adminService.queryAdminByName(adminName);

        if (num == 1) {
            return 1;
        }

        return 0;
    }

    /**
     * 根据条件搜索admin
     *
     * @param searchPageVo
     * @return
     */
    @ApiOperation(value = "搜索admin", notes = "根据条件搜索admin")
    @GetMapping(value = "/adminSearch")
    @ResponseBody
    private Map<String, Object> adminSearch(SearchPageVo searchPageVo) {

        return adminService.searchAdmin(searchPageVo);

    }

    /**
     * 返回edit的初始值
     *
     * @return
     */
    @ApiOperation(value = "查询admin", notes = "根据id查询admin")
    @GetMapping(value = "/getAdminForEdit")
    @ResponseBody
    private Admin getAdminForEdit() {

        return adminService.getAdminById(adminIdTmp);
    }


}
